package com.niit.yhserver.controller;

import com.niit.yhpojo.dto.SalesOrderDTO;
import com.niit.yhpojo.entity.SalesOrder;
import com.niit.yhserver.service.SalesOrderService;
import com.niit.yhserver.result.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/orders")
public class SalesOrderController {

        @Autowired
        private SalesOrderService salesOrderService;
        //显示全部
        @GetMapping("/all")
        public List<SalesOrder> listAllOrders() {
                return salesOrderService.selectAll();
        }

        // 分页查询订单列表
        @GetMapping
        public Result listOrders(
                @RequestParam(required = false) String orderNum,
                @RequestParam(required = false) String userName,
                @RequestParam(required = false) String userPhone,
                @RequestParam(required = false) String status,
                @RequestParam(defaultValue = "1") Integer pageNum,
                @RequestParam(defaultValue = "10") Integer pageSize) {

                return salesOrderService.queryOrderList(orderNum, userName, userPhone, status, pageNum, pageSize);
        }

        // 获取订单详情
        @GetMapping("/{id}")
        public Result getOrderById(@PathVariable Long id) {
                return salesOrderService.getOrderById(id);
        }

        // 创建订单
        @PostMapping
        public Result createOrder(@RequestBody SalesOrderDTO orderDTO) {
                return salesOrderService.createOrder(orderDTO);
        }

        // 更新订单
        @PutMapping("/{id}")
        public Result updateOrder(@PathVariable Long id, @RequestBody SalesOrderDTO orderDTO) {
                try {
                        return salesOrderService.updateOrder(id, orderDTO); // 直接返回service的结果
                } catch (Exception e) {
                        return Result.fail(e.getMessage());
                }
        }

        // 删除订单
        @DeleteMapping("/{id}")
        public Result deleteOrder(@PathVariable Long id) {
                return salesOrderService.deleteOrder(id);
        }
}